package com.queclink.puzzle.common;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author jim.li
 * @email jim.li@queclink.com
 * @date 2022-06-16 17:20:00
 */
public class Place {

    public static int MONTH;

    public static int DAY;

    public static int WEEK;

    public static int runningStatus = 0;

    private static final String initial_label_text = "00:00:00 000";

    public Countingthread thread = new Countingthread();
    // 记录程序开始时间
    public long programstart = System.currentTimeMillis();

    // 程序一开始就是暂停的
    public long pausestart = programstart;
    // 程序暂停的总时间
    public long pausecount = 0;

    public static JButton startButton = new JButton("开始");

    public static boolean stopped = true;

    public class Countingthread extends Thread {

        private Countingthread() {
            setDaemon(true);
        }

        @Override
        public void run() {

            while (true) {
                if (!stopped) {
                    long elapsed = System.currentTimeMillis() - programstart - pausecount;
                    label.setText(format(elapsed));
                }
                try {
                    Thread.sleep(1); // 1毫秒更新一次显示
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.exit(1);
                }
            }
        }

        // 将毫秒数格式化
        private String format(long elapsed) {
            int hour, minute, second, milli;
            milli = (int) (elapsed % 1000);
            elapsed = elapsed / 1000;
            second = (int) (elapsed % 60);
            elapsed = elapsed / 60;
            minute = (int) (elapsed % 60);
            elapsed = elapsed / 60;
            hour = (int) (elapsed % 60);
            return String.format("%02d:%02d:%02d %03d", hour, minute, second, milli);

        }

    }

    public JLabel label = new JLabel(initial_label_text);

    public void placeComponents(JPanel panel) {

        /* 布局部分我们这边不多做介绍
         * 这边设置布局为 null
         */
        panel.setLayout(null);

        // 月
        JLabel monthLabel = new JLabel("月:");
        monthLabel.setBounds(140, 20, 40, 25);
        panel.add(monthLabel);
        JComboBox monthBox = new JComboBox();
        monthBox.setBounds(180, 20, 100, 25);
        for (int i = 0; i < 12; i++) {
            monthBox.addItem(i + 1);
        }
        monthBox.setSelectedIndex(MONTH);
        panel.add(monthBox);


        // 日
        JLabel dayLabel = new JLabel("日:");
        dayLabel.setBounds(140, 50, 40, 25);
        panel.add(dayLabel);
        JComboBox dayBox = new JComboBox();
        dayBox.setBounds(180, 50, 100, 25);
        for (int i = 0; i < 31; i++) {
            dayBox.addItem(i + 1);
        }
        dayBox.setSelectedIndex(DAY - 1);
        panel.add(dayBox);

        // 周
        JLabel weekLabel = new JLabel("周:");
        weekLabel.setBounds(140, 80, 40, 25);
        panel.add(weekLabel);
        JComboBox weekBox = new JComboBox();
        weekBox.setBounds(180, 80, 100, 25);
        for (int i = 0; i < 7; i++) {
            weekBox.addItem(i + 1);
        }
        weekBox.setSelectedIndex(WEEK - 2);
        panel.add(weekBox);

        // 创建登录按钮
        startButton.setBounds(140, 110, 80, 25);
        panel.add(startButton);

        JLabel staticLabel = new JLabel("运行时间:");
        staticLabel.setBounds(140, 150, 80, 25);
        panel.add(staticLabel);

        label.setBounds(200, 150, 120, 25);
        panel.add(label);

        startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (stopped) {
                    pausecount = 0;
                    pausecount += (System.currentTimeMillis() - pausestart);
                    stopped = false;
                    startButton.setText("运行中");
                    Solution solution = SpringUtils.getBean(Solution.class);
                    solution.solution((int) monthBox.getSelectedItem(), (int) dayBox.getSelectedItem(), (int) weekBox.getSelectedItem());
                } else {
                    JOptionPane.showMessageDialog(null, "当前正在运行中，无法暂停（可以关闭重启）！", "提示",JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        thread.start();

    }
}
